<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id?'编辑月卡':'增加月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form label-width="100px" ref="CarInfoForm" :model="CarInfoForm" :rules="CarInfoRules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="CarInfoForm.personName"/>
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="CarInfoForm.phoneNumber" />
            </el-form-item >
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="CarInfoForm.carNumber"/>
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="CarInfoForm.carBrand"/>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form label-width="100px" ref="feeForm" :model="feeForm" :rules="feeFormRules">
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
                v-model="feeForm.payTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd">
            </el-date-picker>
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount"/>
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod">
                <el-option
                  v-for="item in payMethodList"
                  :key="item.value"
                  :value="item.value"
                  :label="item.text"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="confirmForm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import {CreateCardAPI,getDetailAPI,upDateCardAPI} from '@/api/card'
export default {
    data(){
      const validatorCarNumber=(rule,value,callback)=>{
          // console.log(rule,value,callback)
          // value:文本框输入的值
          // callback：回调函数
          const reg=/^[\u4E00-\u9FA5][\da-zA-Z]{6}$/
          if(!reg.test(value)){
            callback(new Error('请输入符合规则的车牌号码'))
          }else{
            callback()
          }
      }
      return{
        CarInfoForm:{
          personName:'',
          phoneNumber:'',
          carNumber:'',
          carBrand:''
        },
        CarInfoRules:{
          personName:[
            {required:true,message:'请输入车主姓名',trigger:'blur'}
          ],
          phoneNumber:[
            {required:true,message:'请输入联系方式',trigger:'blur'}
          ],
          carNumber:[
            {required:true,message:'请输入车牌号码',trigger:'blur'},
            {
              validator:validatorCarNumber,
              trigger:'blur'
            }
          ],

          carBrand:[
            {required:true,message:'请输入车辆品牌',trigger:'blur'}
          ],
        },

        feeForm:{
          paymentAmount:'',
          paymentMethod:null,
          payTime:''
        },
        feeFormRules:{
          paymentAmount:[
            {required:true,message:'请输入支付金额',trigger:'blur'}
          ],
          paymentMethod:[
            {required:true,message:'请输入支付方式',trigger:'change'}
          ],
          payTime:[
            {required:true,message:'请选择支付时间',trigger:'change'}
          ]
        },
        // 支付方式下拉列表
        payMethodList:[
          {
            value:'Alipay',
            text:'支付宝'
          },
          {
            value:'WeChat',
            text:'微信'
          },
          {
            value:'Cash',
            text:'现金'
          }
        ]
      }
    },
    computed:{
      id(){
        return this.$route.query.id
      }
    },
    mounted(){
      if(this.id){
        this.getDetailList()
      }

    },
    methods:{
      async getDetailList(){
       const res=await getDetailAPI(this.id)
       console.log(res)
      //  把对象中的数据结构出来
      const {carBrand,carNumber,personName,phoneNumber,carInfoId,rechargeId}=res.data
      this.CarInfoForm= {carBrand,carNumber,personName,phoneNumber,carInfoId,rechargeId}
      const {cardEndDate,cardStartDate,paymentAmount,paymentMethod}=res.data
      this.feeForm.payTime=[cardStartDate,cardEndDate]
      this.feeForm.paymentAmount=paymentAmount
      this.feeForm.paymentMethod=paymentMethod
      },
      resetForm(){
        this.$refs.CarInfoForm.resetFields()
        this.$refs.feeForm.resetFields()
      },
      confirmForm(){
          this.$refs.CarInfoForm.validate((flag)=>{
              if(flag){//第一个表单通过
                  // 验证第二个表单
                  this.$refs.feeForm.validate(async (flag)=>{
                    if(flag){
                      // 开始调用接口添加数据
                      const FormData={
                        ...this.CarInfoForm,
                        ...this.feeForm,
                        cardStartDate:this.feeForm.payTime[0],
                        cardEndDate:this.feeForm.payTime[1]
                      }
                      delete FormData.payTime
                      // console.log(FormData)
                      if(this.id){
                        await upDateCardAPI(FormData)
                      }else{
                        await CreateCardAPI(FormData)
                      }
                      this.$message.success(`${this.id?'更新成功':'添加成功'}`)
                      this.$router.back()
                    //   console.log(this.CarInfoForm)
                    //   console.log(this.feeForm)
                    }
                  })
              }
          })
      }
    }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
